package cn.syc.struck.demo.http.subscribers;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;


import com.hwangjr.rxbus.RxBus;

import cn.syc.struck.demo.http.progress.ProgressCancelListener;
import cn.syc.struck.demo.http.progress.ProgressDialogHandler;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import rx.Subscriber;

/**
 * 用于在Http请求开始时，自动显示一个ProgressDialog
 * 在Http请求结束是，关闭ProgressDialog
 * 调用者自己对请求数据进行处理
 * Created by czz on 16/3/10.
 */
public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {

    private SubscriberOnNextListener mSubscriberOnNextListener;
    private ProgressDialogHandler mProgressDialogHandler;
    private String tag;

    private Context context;

    public ProgressSubscriber(SubscriberOnNextListener mSubscriberOnNextListener, Context context, String tag) {
        this(mSubscriberOnNextListener, context, tag, true);
    }

    public ProgressSubscriber(SubscriberOnNextListener mSubscriberOnNextListener, Context context, String tag, boolean cancelable) {
        this.mSubscriberOnNextListener = mSubscriberOnNextListener;
        this.context = context;
        this.tag = tag;
        mProgressDialogHandler = new ProgressDialogHandler(context, this, cancelable);
    }

    private void showProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {
        showProgressDialog();
    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        dismissProgressDialog();
        //Toast.makeText(context, "Data Loading Completed", Toast.LENGTH_SHORT).show();
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     *
     * @param e
     */
    @Override
    public void onError(Throwable e) {
        Log.d("tt", e.toString());
        //int errcode = 0;
        String txt = "";
        if (e instanceof SocketTimeoutException) {
            //Toast.makeText(context, "网络连接超时，请检查您的网络及服务器状态", Toast.LENGTH_SHORT).show();
            txt = "网络连接超时，请检查您的网络及服务器状态";
            //errcode = 400;
        } else if (e instanceof ConnectException) {
            //Toast.makeText(context, "连接服务器失败，请检查您的网络", Toast.LENGTH_SHORT).show();
            txt = "连接服务器失败，请检查您的网络";
            //errcode = 401;
        } else if (e instanceof java.net.UnknownHostException) {
            //Toast.makeText(context, "未知的服务器地址，请检查服务器状态", Toast.LENGTH_SHORT).show();
            txt = "未知的服务器地址，请检查服务器状态";
            //errcode = 402;
        } else {
            //Toast.makeText(context, "error:" + e.getMessage(), Toast.LENGTH_SHORT).show();
            //errcode = 450;
            txt = "error:" + e.getMessage();
        }
        RxBus.get().post(tag, txt);
        dismissProgressDialog();

    }

    /**
     * 将onNext方法中的返回结果交给Activity或Fragment自己处理
     *
     * @param t 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(T t) {
        if (mSubscriberOnNextListener != null) {
            mSubscriberOnNextListener.onNext(t);
        }
    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}